const $math = {
  // 乘法
  mul (arg1, arg2) {
    const r1 = arg1.toString();
    const r2 = arg2.toString();
    const d = arguments[2];
    const m = (r1.split('.')[1] ? r1.split('.')[1].length : 0) + (r2.split('.')[1] ? r2.split('.')[1].length : 0);
    const resultVal = Number(r1.replace('.', '')) * Number(r2.replace('.', '')) / Math.pow(10, m);
    return typeof d !== 'number' ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
  },
  div: function (arg1, arg2) {
    const r1 = arg1.toString();
    const r2 = arg2.toString();
    const d = arguments[2];
    const m = (r2.split('.')[1] ? r2.split('.')[1].length : 0) - (r1.split('.')[1] ? r1.split('.')[1].length : 0);
    const resultVal = Number(r1.replace('.', '')) / Number(r2.replace('.', '')) * Math.pow(10, m);
    return typeof d !== 'number' ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)))
  },
  add: function (arg1, arg2) {
    arg1 = arg1.toString();
    arg2 = arg2.toString();
    const arg1Arr = arg1.split('.');
    const arg2Arr = arg2.split('.');
    const d1 = arg1Arr.length === 2 ? arg1Arr[1] : '';
    const d2 = arg2Arr.length === 2 ? arg2Arr[1] : '';
    const maxLen = Math.max(d1.length, d2.length);
    const m = Math.pow(10, maxLen);
    const result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
    const d = arguments[2];
    return typeof d === 'number' ? Number((result).toFixed(d)) : result
  },
  sub: function (arg1, arg2) {
    return this.add(arg1, -Number(arg2), arguments[2])
  }
};
export default $math;
